#include "infoshowserver.h"
#include "ui_infoshowserver.h"
#include "rolltext.h"
#include <QDebug>
#include "ui_form.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <formenvir.h>
InfoShowServer::InfoShowServer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InfoShowServer)
{
    ui->setupUi(this);

    connect(&mprocess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(start_process(int,QProcess::ExitStatus)));


    //mplayer.exe -slave -wid id号 1.mp4 //把mplayer嵌入到qt界面中
        QStringList arglist;
        arglist<<"-slave";  //mplayer从模式可以接终端写入的数据（管道通信）
        arglist<<"-wid";
        arglist<<QString::number(ui->PicOrVid->winId());
    #if WIN32
        arglist<<"C:/Users/asus/Music/MV/3.mp4";
        mprocess.start("C:/Users/asus/Desktop/qt/MPlayer for Windows/mplayer.exe",arglist);
    #else
        arglist<<"/mnt/udisk/Video/AgainSeeYou.mp4";
        mprocess.start("mplayer",arglist);
#endif

    //读数据信号
    connect(&msocket,&QTcpSocket::readyRead,this,&InfoShowServer::read_data);
   // connect(&msocket_notice,SIGNAL(mysignal()),this,SLOT(read_data_notice()));


    //天气


    //关联请求finished信号
    connect(&manager, &QNetworkAccessManager::finished,this,&InfoShowServer::read_weatherdata);
    //天气显示界面
    items = new Form[1];
    //把items添加到widget容器里面的weatherHBox中
    for(int i=0; i<1; i++)
    {
        ui->weatherHBox->addWidget(&items[i]);
    }

    //环境显示界面
    connect(&manager_envir,&QNetworkAccessManager::finished,this,&InfoShowServer::read_environment);
    items_envir = new FormEnvir[1];
    for(int i = 0;i<1;i++)
    {
        ui->environmentHBox->addWidget(&items_envir[i]);
    }

    //时间显示
    //关联线程信号sendData
    connect(&showTimeThread,&ShowTime::sendData,this, &InfoShowServer::update_time);
    //启动线程
    showTimeThread.start();

}

InfoShowServer::~InfoShowServer()
{
    delete ui;
}


void InfoShowServer::start_process(int, QProcess::ExitStatus)
{

}

void InfoShowServer::on_connectBt_clicked()
{
    msocket.connectToHost(ui->ipEdit->text(),ui->portEdit->text().toUShort());

}

void InfoShowServer::read_data()
{
    QString data = msocket.readAll();
    //ui->label->setText(data);
    ui->roll_text->setText(data);
    //qDebug()<<"222";

}




void InfoShowServer::read_weatherdata(QNetworkReply *reply)
{
    QString data = reply->readAll();
    //qDebug()<<data;

    //json解析--QJson
    QJsonParseError err;
    QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8(),&err);
    if(err.error != QJsonParseError::NoError)
    {
        qDebug()<<"json error";
    }

    QJsonObject obj = doc.object();

    qDebug()<<obj.value("cityid");
    //取到data数组
    QJsonArray dataArray = obj.value("data").toArray();
    qDebug()<<dataArray.size();

    QString date,wea,tem1,tem2,pressure,sunrise,sunset,air_level,win_speed;

    if(dataArray.size()>0)
    {
        //data数组的第一个对象
        QJsonObject dataArrayObj = dataArray.at(0).toObject();

        date = dataArrayObj.value("date").toString();
        wea = dataArrayObj.value("wea").toString();
        tem1 = dataArrayObj.value("tem1").toString();
        tem2 = dataArrayObj.value("tem2").toString();
        pressure = dataArrayObj.value("pressure").toString();
        sunrise = dataArrayObj.value("sunrise").toString();
        sunset = dataArrayObj.value("sunset").toString();
        air_level = dataArrayObj.value("air_level").toString();
        win_speed = dataArrayObj.value("win_speed").toString();

    }

    for(int i = 0;i<1;i++)
    {
        items[i].set_weather(date,wea,tem1,tem2,pressure);
        items_envir[i].set_environment(sunrise,sunset,air_level,win_speed);
    }

}

void InfoShowServer::read_environment(QNetworkReply *reply)
{

}

void InfoShowServer::update_time(QString t)
{
    ui->label->setText(t);
}

void InfoShowServer::on_updateBt_clicked()
{
    //获取城市名
    QString cityname = ui->cityEdit->text();
    qDebug()<<cityname;
    //qDebug()<<cityname<<citykey.value(cityname);
    //QString urlstr = QString("http://t.weather.sojson.com/api/weather/city/%1")
            //.arg(citykey.value(cityname));
    //QString urlstr = QString("https://v0.yiketianqi.com/api?version=v9&appid=87915418&appsecret=9FKG5n1c").arg("101271201");
    QUrl url(QString("https://v0.yiketianqi.com/api?version=v91&appid=87915418&appsecret=9FKG5n1c&city=%1").arg(cityname));
    //请求天气数据
    //QUrl url(urlstr);
    QNetworkRequest request(url);
    manager.get(request);
}
